﻿namespace WMS6.Web.Inventory
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.InventoryMgtServiceRef;
    using WMS6.Web.UserControls;

    public class LpnList : BasePage
    {
        protected HtmlInputButton btnAction;
        protected Button btnDelete;
        protected Button btnEdit;
        protected Button btnExport;
        protected Button btnList;
        protected Button btnNew;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F5076;
        protected GridView gvLpn;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnLpnIDs;
        protected HtmlInputHidden hdnRtnValue;
        protected HtmlHead Head1;
        protected Label lblLpnList;
        protected Label lblMsg;
        protected LinkButton lnkbtnLpnSplit;
        protected LinkButton lnkbtnPrintLpnLabel;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected HtmlTableCell tdlnkbtnLpnSplit;
        protected HtmlTableCell tdlnkbtnPrintLpnLabel;

        private void BindDataLpnList(LpnQueryEntity LpnEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(LpnEntity.CurrentPage);
            DataSet ds = new DataSet();
            ds = InventoryMgtServiceClientProxy.GetInstance().GetLpnByQueryList(LpnEntity);
            base.BindData2GridView(this.gvLpn, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> selectedID = base.GetSelectedIDsInGridView(this.gvLpn, "LPN_ID");
                List<string> qty = base.GetSelectedIDsInGridView(this.gvLpn, "Qty");
                if ((qty != null) && (qty.Count > 0))
                {
                    for (int i = 0; qty.Count > i; i++)
                    {
                        if (!(string.IsNullOrEmpty(qty[i]) || (decimal.Parse(qty[i]) <= 0M)))
                        {
                            this.lblMsg.Text = "lpn" + selectedID[i] + "数量大于0无法删除！";
                            return;
                        }
                    }
                }
                if ((selectedID == null) || (selectedID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F4100_035");
                }
                else
                {
                    this.lblMsg.Text = InventoryMgtServiceClientProxy.GetInstance().DeleteLpn(selectedID) + base.GetGlobalMsgResource("F4100_012");
                    LpnQueryEntity lpnQuery = this.GetLpnQueryEntity();
                    this.BindDataLpnList(lpnQuery);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F4100_013") + ex.Message;
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvLpn, "LPN_ID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F4100_035");
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F4100_036");
            }
            else
            {
                base.Response.Redirect(string.Format("LPN.aspx?from=list&ID={0}", base.UrlEncode(selectedID[0])));
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            LpnQueryEntity lpnQuery = this.GetLpnQueryEntity();
            lpnQuery.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            this.BindDataLpnList(lpnQuery);
            this.gvLpn.ShowFooter = false;
            lpnQuery.IsGetAll = false;
            this.SetLpnQueryEntity(lpnQuery);
            this.hdnActionID.Value = "";
            base.Export2Excel("Lpn.xls", this.gvLpn);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            LpnQueryEntity LpnEntity = this.GetLpnQueryEntity();
            LpnEntity.CurrentPage = "0";
            this.BindDataLpnList(LpnEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            LpnQueryEntity LpnEntity = this.GetLpnQueryEntity();
            LpnEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindDataLpnList(LpnEntity);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            LpnQueryEntity lpnQuery = this.GetLpnQueryEntity();
            lpnQuery.LpnID = "";
            lpnQuery.PackID = "";
            lpnQuery.DocID = "";
            lpnQuery.DocType = "";
            lpnQuery.ExternID = "";
            lpnQuery.OwnerID = "";
            lpnQuery.SkuID = "";
            lpnQuery.History = "N";
            this.BindDataLpnList(lpnQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            LpnQueryEntity LpnEntity = this.GetLpnQueryEntity();
            if (int.Parse(LpnEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                LpnEntity.CurrentPage = (int.Parse(LpnEntity.CurrentPage) + 1).ToString();
                this.BindDataLpnList(LpnEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            LpnQueryEntity LpnEntity = this.GetLpnQueryEntity();
            if (int.Parse(LpnEntity.CurrentPage) > 0)
            {
                LpnEntity.CurrentPage = (int.Parse(LpnEntity.CurrentPage) - 1).ToString();
                this.BindDataLpnList(LpnEntity);
            }
        }

        protected void btnSearch_Click(object sernder, EventArgs e)
        {
            base.Server.Transfer("LpnQuery.aspx", false);
        }

        private LpnQueryEntity GetLpnQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as LpnQueryEntity);
            }
            return (base.PageQueryEntitySession as LpnQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                LpnQueryEntity LpnEntity = this.GetLpnQueryEntity();
                LpnEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindDataLpnList(LpnEntity);
            }
        }

        protected void gvLpn_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F5076", this.gvLpn);
        }

        protected void gvLpn_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
            }
        }

        protected void gvLpn_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            string strValue = base.UrlReplace(this.gvLpn.DataKeys[e.NewSelectedIndex]["LPN_ID"].ToString());
            if (!string.IsNullOrEmpty(strValue))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'> window.returnValue ='" + strValue + "';  window.close();</script>", false);
            }
        }

        protected void gvLpn_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                LpnQueryEntity LpnEntity = this.GetLpnQueryEntity();
                LpnEntity.SortField = e.SortExpression;
                if (LpnEntity.SortDirection == "ASC")
                {
                    LpnEntity.SortDirection = "DESC";
                }
                else if (LpnEntity.SortDirection == "DESC")
                {
                    LpnEntity.SortDirection = "ASC";
                }
                this.BindDataLpnList(LpnEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void lnkbtnLpnSplit_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvLpn, "LPN_ID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F4100_035");
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F4100_036");
            }
            else
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>popLpmSplit('" + selectedID[0] + "');</script>", false);
            }
        }

        protected void lnkbtnPrintLpnLabel_Click(object sender, EventArgs e)
        {
            string lpns = string.Empty;
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvLpn, "LPN_ID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F4100_035");
            }
            else
            {
                for (int i = 0; selectedID.Count > i; i++)
                {
                    string CS00001 = lpns;
                    lpns = CS00001 + (string.IsNullOrEmpty(lpns) ? "" : ",") + "'" + selectedID[i] + "'";
                }
                this.hdnLpnIDs.Value = lpns;
                this.Session["PrintLpnIDS"] = lpns;
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>popPrinLpnLabel();</script>", false);
            }
        }

        private void LoadData()
        {
            LpnQueryEntity lpnQuery = new LpnQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    lpnQuery.LpnID = this.PreviousPage.LpnID;
                    lpnQuery.PackID = this.PreviousPage.PackID;
                    lpnQuery.DocID = this.PreviousPage.DocID;
                    lpnQuery.DocType = this.PreviousPage.DocType;
                    lpnQuery.ExternID = this.PreviousPage.ExternID;
                    lpnQuery.IsActive = this.PreviousPage.IsActive;
                    lpnQuery.OwnerID = this.PreviousPage.OwnerID;
                    lpnQuery.SkuID = this.PreviousPage.SkuID;
                    lpnQuery.History = this.PreviousPage.History;
                }
                else
                {
                    lpnQuery.LpnID = "";
                    lpnQuery.PackID = "";
                    lpnQuery.DocID = "";
                    lpnQuery.DocType = "";
                    lpnQuery.ExternID = "";
                    lpnQuery.IsActive = "";
                    lpnQuery.OwnerID = "";
                    lpnQuery.SkuID = "";
                    lpnQuery.History = "N";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"];
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["action"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["action"];
                this.gvLpn.Columns[1].Visible = false;
                this.gvLpn.Columns[2].Visible = true;
            }
            lpnQuery.IsGetAll = false;
            lpnQuery.CurrentPage = "0";
            lpnQuery.PageSize = this.gvLpn.PageSize.ToString();
            lpnQuery.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvLpn.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetLpnQueryEntity(lpnQuery);
            this.BindDataLpnList(lpnQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetLpnQueryEntity(LpnQueryEntity lpnQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = lpnQuery;
            }
            else
            {
                base.PageQueryEntitySession = lpnQuery;
            }
        }

        public LpnQuery PreviousPage
        {
            get
            {
                return (LpnQuery) base.PreviousPage;
            }
        }
    }
}

